﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class GoodsNumber
    {
        public int Add(Chain.Model.GoodsNumber model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GoodsNumber(");
            builder.Append("GoodsID,ShopID,Number)");
            builder.Append(" values (");
            builder.Append("@GoodsID,@ShopID,@Number)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4), new SqlParameter("@Number", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GoodsID;
            cmdParms[1].Value = model.ShopID;
            cmdParms[2].Value = model.Number;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool ChenkOutShopGoodsNumber(int OutShopID, int GoodsID, decimal OutNumber)
        {
            int num = 0;
            DataSet set = DbHelperSQL.Query(string.Format("select Number from GoodsNumber where GoodsID={0} and ShopID={1}", GoodsID, OutShopID));
            if (set.Tables[0].Rows.Count > 0)
            {
                num = Convert.ToInt32(set.Tables[0].Rows[0][0]);
            }
            return (num >= OutNumber);
        }

        public DataSet CkeckInShopGoods(int intGoodsID, int intShopID)
        {
            return DbHelperSQL.Query(string.Format("select * from  dbo.GoodsNumber  where GoodsID={0} and  ShopID={1}", intGoodsID, intShopID));
        }

        public bool Delete(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsNumber ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string IDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsNumber ");
            builder.Append(" where ID in (" + IDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool DeleteNumber(int GoodsID, int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsNumber ");
            builder.Append(" where GoodsID=@GoodsID");
            builder.Append(" and ShopID=@ShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GoodsID;
            cmdParms[1].Value = ShopID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GoodsNumber");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetAllGoodsidByShopID(int ShopID)
        {
            return DbHelperSQL.Query(string.Format("select GoodsID from GoodsNumber where ShopID={0}", ShopID));
        }

        public int GetGoodsCount(int ClassID)
        {
            string format = "select count(1) FROM Goods where GoodsClassID = {0}";
            return Convert.ToInt32(DbHelperSQL.GetSingle(string.Format(format, ClassID).ToString()));
        }

        public int GetGoodsCount(int ClassID, int ShopID)
        {
            string format = "select count(1) FROM Goods,GoodsNumber where goods.GoodsID = GoodsNumber.GoodsID and goods.GoodsClassID = {0} and GoodsNumber.ShopID = {1}";
            return Convert.ToInt32(DbHelperSQL.GetSingle(string.Format(format, ClassID, ShopID).ToString()));
        }

        public DataSet GetGoodsNumber(int intGoodsID, int intShopID)
        {
            return DbHelperSQL.Query(string.Concat(new object[] { " select * from V_GoodsNumber where ShopID=", intShopID, " and GoodsID=", intGoodsID }).ToString());
        }

        public DataSet GetGoodsNumber(string strGoodsCode, int intShopID)
        {
            return DbHelperSQL.Query(string.Concat(new object[] { " select * from V_GoodsNumber where ShopID=", intShopID, " and GoodsCode='", strGoodsCode, "'" }).ToString());
        }

        public DataSet GetGoodsNumberDelServe(string strGoodsCode, int intShopID)
        {
            return DbHelperSQL.Query(string.Concat(new object[] { " select * from V_GoodsNumber where GoodsType = 0 AND ShopID=", intShopID, " and GoodsCode='", strGoodsCode, "'" }).ToString());
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ID,GoodsID,ShopID,Number ");
            builder.Append(" FROM GoodsNumber ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ID,GoodsID,ShopID,Number ");
            builder.Append(" FROM GoodsNumber ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ID desc");
            }
            builder.Append(")AS Row, T.*  from GoodsNumber T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_GoodsNumber";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "GoodsCreateTime", "GoodsID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "GoodsNumber");
        }

        public Chain.Model.GoodsNumber GetModel(int ID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ID,GoodsID,ShopID,Number from GoodsNumber ");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ID;
            Chain.Model.GoodsNumber number = new Chain.Model.GoodsNumber();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["ID"] != null) && (set.Tables[0].Rows[0]["ID"].ToString() != ""))
            {
                number.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsID"] != null) && (set.Tables[0].Rows[0]["GoodsID"].ToString() != ""))
            {
                number.GoodsID = int.Parse(set.Tables[0].Rows[0]["GoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["ShopID"] != null) && (set.Tables[0].Rows[0]["ShopID"].ToString() != ""))
            {
                number.ShopID = int.Parse(set.Tables[0].Rows[0]["ShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Number"] != null) && (set.Tables[0].Rows[0]["Number"].ToString() != ""))
            {
                number.Number = int.Parse(set.Tables[0].Rows[0]["Number"].ToString());
            }
            return number;
        }

        public decimal GetNumber(int goodsID, int shopID)
        {
            object single = DbHelperSQL.GetSingle(string.Format("select Number from GoodsNumber where GoodsID={0} and ShopID={1}", goodsID, shopID));
            if (single != null)
            {
                return decimal.Parse(single.ToString());
            }
            return 0M;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GoodsNumber ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetShopIDListByGoods(int GoodsID)
        {
            return DbHelperSQL.Query("select shopid from GoodsNumber where GoodsID = " + GoodsID);
        }

        public void InitShopData()
        {
            StringBuilder builder = new StringBuilder(200);
            builder.Append(" INSERT INTO GoodsNumber (GoodsID,ShopID,Number) ");
            builder.Append(" select GTemp.GoodsID AS GID,STemp.ShopID AS SID,0 from ");
            builder.Append(" (SELECT ShopID FROM SysShop WHERE ShopID NOT IN(SELECT ShopID FROM GoodsNumber)) AS STemp  , ");
            builder.Append(" (select GoodsID FROM Goods  ) AS GTemp ");
            DbHelperSQL.ExecuteSql(builder.ToString());
        }

        public int InsertGoodsNumber(int intGoodsID)
        {
            return DbHelperSQL.ExecuteSql((" insert into GoodsNumber (GoodsID,ShopID,Number) select " + intGoodsID.ToString() + ", SysShop.ShopID, 0 from SysShop where SysShop.ShopID>0").ToString());
        }

        public int InsertGoodsNumber(int intGoodsID, int intShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GoodsNumber(");
            builder.Append("GoodsID,ShopID,Number)");
            builder.Append("values(");
            builder.Append("@GoodsID,@ShopID,0)");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4) };
            cmdParms[0].Value = intGoodsID;
            cmdParms[1].Value = intShopID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }

        public int SyncGoods(int GoodsID)
        {
            string format = "insert into GoodsNumber(GoodsID,ShopID,Number) \r\n                (select {0},ShopID,0 from SysShop where ShopID>0 and ShopID not in(SELECT ShopID FROM GoodsNumber where GoodsID = {0}))";
            return DbHelperSQL.ExecuteSql(string.Format(format, GoodsID));
        }

        public int SyncGoods(int GoodsID, List<int> ShopList)
        {
            if (ShopList.Count <= 0)
            {
                return 0;
            }
            string str = string.Empty;
            foreach (int num in ShopList)
            {
                str = str + num + ",";
            }
            str = str.Trim(new char[] { ',' });
            string format = "insert into GoodsNumber(GoodsID,ShopID,Number) \r\n                (select {0},ShopID,0 from SysShop where ShopID>0 and ShopID in({1}) and ShopID not in(SELECT ShopID FROM GoodsNumber where GoodsID = {0}))";
            return DbHelperSQL.ExecuteSql(string.Format(format, GoodsID, str));
        }

        public int SyncGoods(int GoodsID, int ShopID)
        {
            string format = "insert into GoodsNumber(GoodsID,ShopID,Number) \r\n                (select {0},ShopID,0 from SysShop where ShopID>0 and ShopID={1} and ShopID not in(SELECT ShopID FROM GoodsNumber where GoodsID = {0}))";
            return DbHelperSQL.ExecuteSql(string.Format(format, GoodsID, ShopID));
        }

        public int UpdataGoodsNumber(Chain.Model.GoodsNumber model)
        {
            return DbHelperSQL.ExecuteSql(string.Concat(new object[] { " update GoodsNumber set Number =CAST(ISNULL(Number,0)AS MONEY)+CAST(", model.Number, " AS MONEY) where GoodsID =", model.GoodsID, " and ShopID=", model.ShopID }).ToString());
        }

        public bool UPdataInShopGoodsNumber(int OutShopID, int GoodsID, int OutNumber)
        {
            return (DbHelperSQL.ExecuteSql(string.Format("Update GoodsNumber set Number=Number+{0} where GoodsID={1} and ShopID={2}", OutNumber, GoodsID, OutShopID)) > 0);
        }

        public bool UPdataOutShopGoodsNumber(int OutShopID, int GoodsID, decimal OutNumber)
        {
            return (DbHelperSQL.ExecuteSql(string.Format("Update GoodsNumber set Number=Number-{0} where GoodsID={1} and ShopID={2}", OutNumber, GoodsID, OutShopID)) > 0);
        }

        public bool Update(Chain.Model.GoodsNumber model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GoodsNumber set ");
            builder.Append("GoodsID=@GoodsID,");
            builder.Append("ShopID=@ShopID,");
            builder.Append("Number=@Number");
            builder.Append(" where ID=@ID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsID", SqlDbType.Int, 4), new SqlParameter("@ShopID", SqlDbType.Int, 4), new SqlParameter("@Number", SqlDbType.Int, 4), new SqlParameter("@ID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.GoodsID;
            cmdParms[1].Value = model.ShopID;
            cmdParms[2].Value = model.Number;
            cmdParms[3].Value = model.ID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int UpdateGoodsNumber(int goodsID, decimal goodsNumber, int shopID)
        {
            return DbHelperSQL.ExecuteSql(string.Format(" Update GoodsNumber set Number={0} where GoodsID={1} and ShopID={2}", goodsNumber, goodsID, shopID).ToString());
        }
    }
}

